//
//  JNTTabBarController.m
//  
//
//  Created by 王文晓 on 16/8/4.
//
//

#import "JNTTabBarController.h"
#import "JNTHomeViewController.h"
#import "JNTWorkbenchViewController.h"
#import "JNTAddressBookViewController.h"
#import "JNTMailViewController.h"
#import "ZTNavigationController.h"


@interface JNTTabBarController ()<UINavigationControllerDelegate>

@property(nonatomic,strong)JNTHomeViewController *home;

@property(nonatomic,strong)JNTWorkbenchViewController *Workbench;
@property(nonatomic,strong)JNTAddressBookViewController *AddressBook;

@property(nonatomic,strong)JNTMailViewController *Mail;

@property(nonatomic,strong)NSMutableArray *itemArr;


@property (nonatomic, weak) id popDelegate;


@end

@implementation JNTTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.selectedIndex = 0;
//   [[UITabBar appearance] setBackgroundColor:[UIColor blueColor]];
//    self.tabBar.backgroundColor =[UIColor blueColor];
  [UITabBar appearance].translucent = NO;
    UIView *view = [[UIView alloc]init];
    view.backgroundColor =  [UIColor colorWithRed:71/255.0 green:158/255.0  blue:239/255.0  alpha:1];
    view.frame = self.tabBar.bounds;
    [[UITabBar appearance]insertSubview:view atIndex:0];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [self setUpAllChildViewController];
}

- (void)setUpAllChildViewController
{
    //首页
    JNTHomeViewController *home = [[JNTHomeViewController alloc] init];
    home.dataSourceMArr = self.dataSourceMArr;
    home.isFirst = self.isFirst;
    NSLog(@"%@", home.dataSourceMArr);
    NSLog(@"%@", self.dataSourceMArr);
    NSLog(@"houArray===%lu",(unsigned long)self.dataSourceMArr.count);
    [self setUpOneChildViewController:home image:[UIImage imageNamed:@"th_btn_home_bottom_s"] selectedImage:[UIImage imageWithOriginalName:@"th_btn_home_bottom_f"] title:@"首页"];
    _home = home;
    
    //工作台
    JNTWorkbenchViewController *remind = [[JNTWorkbenchViewController alloc] init];
    
    [self setUpOneChildViewController:remind image:[UIImage imageNamed:@""] selectedImage:[UIImage imageWithOriginalName:@""] title:@"工作台"];
//    remind.flag = @"tabbar";
    //通讯录
    JNTAddressBookViewController *zixun = [[JNTAddressBookViewController alloc] init];
    [self setUpOneChildViewController:zixun image:[UIImage imageNamed:@""] selectedImage:[UIImage imageWithOriginalName:@""] title:@"通讯录"];
//    zixun.flag = @"tabbar";
    //邮件
    JNTMailViewController *service = [[JNTMailViewController alloc] init];
    [self setUpOneChildViewController:service image:[UIImage imageNamed:@""] selectedImage:[UIImage imageWithOriginalName:@""] title:@"邮件"];
    service = service;
    

    
}
#pragma mark - 当点击tabBar上的按钮调用
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    self.selectedIndex = item.tag;
}
#pragma mark - 添加一个子控制器
- (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title
{
    vc.title = title;
    
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectedImage;
    
    
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    

    //未选中字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor],NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:16.0]} forState:UIControlStateNormal];
    
    //选中字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:16.0]} forState:UIControlStateSelected];

    // 保存tabBarItem模型到数组
    [self.itemArr addObject:vc.tabBarItem];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    _popDelegate = nav.interactivePopGestureRecognizer.delegate;
    nav.delegate = self;
    [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"上栏"] forBarMetrics:UIBarMetricsDefault];
    //导航标题颜色
    [nav.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.automaticallyAdjustsScrollViewInsets = YES;
    nav.navigationBar.translucent = NO;
    
    [self addChildViewController:nav];
}


- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == self.viewControllers[0]) { // 是根控制器
        
        navigationController.interactivePopGestureRecognizer.delegate = nil;
        
    }else{ // 非根控制器
        navigationController.interactivePopGestureRecognizer.delegate = _popDelegate;
        
    }
}



- (NSMutableArray *)dataSourceMArr{
    
    if (!_dataSourceMArr) {
        
        _dataSourceMArr = [NSMutableArray array];
    }
    return _dataSourceMArr;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
